#   SCE CONFIDENTIAL
#   PLAYSTATION(R)3 Programmer Tool Runtime Library 192.001
#   Copyright (C) 2007 Sony Computer Entertainment Inc.
#   All Rights Reserved.
#


#----------------------------------------------------------------------
# SPU targets
#----------------------------------------------------------------------

SPU_C_SRCS          =   $(filter %.c,   $(SPU_SRCS))
SPU_CC_SRCS         =   $(filter %.cc,  $(SPU_SRCS))
SPU_CXX_SRCS        =   $(filter %.cpp, $(SPU_SRCS))
SPU_AS_SRCS         =   $(filter %.s,   $(SPU_SRCS))
SPU_BIN_SRCS        =   $(filter %.bin, $(SPU_SRCS))
SPU_ELF_SRCS        =   $(filter %.elf, $(SPU_SRCS))
SPU_AR_SRCS         =   $(filter %.a,   $(SPU_SRCS))

SPU_C_OBJS          =   $(patsubst %, $(OBJS_DIR)/%.spu.o, $(basename $(SPU_C_SRCS)))
SPU_CC_OBJS         =   $(patsubst %, $(OBJS_DIR)/%.spu.o, $(basename $(SPU_CC_SRCS)))
SPU_CXX_OBJS        =   $(patsubst %, $(OBJS_DIR)/%.spu.o, $(basename $(SPU_CXX_SRCS)))
SPU_AS_OBJS         =   $(patsubst %, $(OBJS_DIR)/%.spu.o, $(basename $(SPU_AS_SRCS)))
SPU_BIN_OBJS        =   $(patsubst %, $(OBJS_DIR)/%.spu.o, $(basename $(SPU_BIN_SRCS)))
SPU_ELF_OBJS        =   $(patsubst %, $(OBJS_DIR)/%.spu.o, $(basename $(SPU_ELF_SRCS)))
SPU_EMBEDDED_OBJS   =   $(patsubst %, $(OBJS_DIR)/%.spu.o, $(SPU_EMBEDDED_SRCS))
SPU_OBJS            =   \
                        $(SPU_C_OBJS) \
                        $(SPU_CC_OBJS) \
                        $(SPU_CXX_OBJS) \
                        $(SPU_AS_OBJS) \
                        $(SPU_BIN_OBJS) \
                        $(SPU_ELF_OBJS) \
                        $(SPU_EMBEDDED_OBJS)

SPU_OBJS_DEPENDS    :=   $(SPU_SRCS)
SPU_OBJS_DEPENDS    :=   $(patsubst %.c,   $(OBJS_DIR)/$(basename %).spu.o, $(SPU_OBJS_DEPENDS))
SPU_OBJS_DEPENDS    :=   $(patsubst %.cc,  $(OBJS_DIR)/$(basename %).spu.o, $(SPU_OBJS_DEPENDS))
SPU_OBJS_DEPENDS    :=   $(patsubst %.cpp, $(OBJS_DIR)/$(basename %).spu.o, $(SPU_OBJS_DEPENDS))
SPU_OBJS_DEPENDS    :=   $(patsubst %.s,   $(OBJS_DIR)/$(basename %).spu.o, $(SPU_OBJS_DEPENDS))
SPU_OBJS_DEPENDS    :=   $(patsubst %.bin, $(OBJS_DIR)/$(basename %).spu.o, $(SPU_OBJS_DEPENDS))
SPU_OBJS_DEPENDS    :=   $(patsubst %.elf, $(OBJS_DIR)/$(basename %).spu.o, $(SPU_OBJS_DEPENDS))

SPU_AR_OBJS         =   $(patsubst %, $(OBJS_DIR)/%/*.o, $(basename $(SPU_AR_SRCS)))
SPU_AR_DEPENDS      =   $(patsubst %, $(OBJS_DIR)/%/libtmp.a, $(basename $(SPU_AR_SRCS)))

SPU_C_MD_FILES      =   $(patsubst %, $(OBJS_DIR)/%.spu.d, $(basename $(SPU_C_SRCS)))
SPU_CC_MD_FILES     =   $(patsubst %, $(OBJS_DIR)/%.spu.d, $(basename $(SPU_CC_SRCS)))
SPU_CXX_MD_FILES    =   $(patsubst %, $(OBJS_DIR)/%.spu.d, $(basename $(SPU_CXX_SRCS)))
SPU_AS_MD_FILES     =   $(patsubst %, $(OBJS_DIR)/%.spu.d, $(basename $(SPU_AS_SRCS)))
SPU_MD_FILES        =   \
                        $(SPU_C_MD_FILES) \
                        $(SPU_CC_MD_FILES) \
                        $(SPU_CXX_MD_FILES) \
                        $(SPU_AS_MD_FILES)

ifneq ($(SPU_TARGET),)
SPU_SELF_TARGET     =   $(basename $(SPU_TARGET))$(FSELF_SUFFIX)
endif
SPU_CLEAN_OBJS      = $(strip \
                        $(SPU_OBJS) $(SPU_MD_FILES) $(SPU_TARGET) $(SPU_SELF_TARGET) \
                        $(SPU_LIB_TARGET) $(SPU_INCLINK_TARGET))

TARGET              +=  $(SPU_TARGET) $(SPU_SELF_TARGET) $(SPU_LIB_TARGET) $(SPU_INCLINK_TARGET)

SPU_LINK_OBJS       = $(strip \
                        $(SPU_LDLIBDIR) $(SPU_OBJS) $(SPU_AR_OBJS) \
                        $(SPU_LIBS) $(SPU_LOADLIBS) $(SPU_LDLIBS))

#----------------------------------------------------------------------
# check options
#----------------------------------------------------------------------
ifneq ($(strip $(SPU_LOADLIBS)),)
	SPU_LOADLIBS := -Wl,--start-group $(SPU_LOADLIBS) -Wl,--end-group
endif

# -O0 and -Wuninitialized are exclusive
ifeq ($(SPU_OPTIMIZE_LV),-O0)
	WARNFLAGS_UNINITIALIZED =
endif

#----------------------------------------------------------------------
# Build rules for SPU
#----------------------------------------------------------------------
$(SPU_TARGET): $(SPU_DEPENDS) $(SPU_OBJS_DEPENDS) $(SPU_LIBS)
	@mkdir -p $(dir $(@))
	$(SPU_CXXLD) $(SPU_LDFLAGS) $(SPU_LINK_OBJS) -o $@

$(SPU_SELF_TARGET): $(SPU_TARGET)
	@mkdir -p $(dir $(@))
ifneq ($(MAKE_FSELF_TYPE),)
	$(MAKE_FSELF) -input $< -output $@ $(MAKE_FSELF_TYPE)
else
	$(MAKE_FSELF) $< $@
endif

$(SPU_LIB_TARGET): $(SPU_DEPENDS) $(SPU_OBJS_DEPENDS) $(SPU_AR_DEPENDS)
	@mkdir -p $(dir $(@))
	$(SPU_AR) r $@ $(SPU_OBJS) $(SPU_AR_OBJS)
	$(SPU_RANLIB) $@

$(SPU_INCLINK_TARGET): $(SPU_DEPENDS) $(SPU_OBJS_DEPENDS) $(SPU_LIBS)
	@mkdir -p $(dir $(@))
	$(SPU_CXXLD) -r -nostartfiles $(SPU_LDFLAGS) $(SPU_LINK_OBJS) -o $@

$(SPU_C_OBJS): $(OBJS_DIR)/%.spu.o: %.c
	@mkdir -p $(dir $(@))
	$(SPU_CC) -c $(SPU_CFLAGS) -Wp,-MMD,$(@:.o=.d),-MT,$@ -o $@ $<

$(SPU_CC_OBJS): $(OBJS_DIR)/%.spu.o: %.cc
	@mkdir -p $(dir $(@))
	$(SPU_CXX) -c $(SPU_CXXFLAGS) -Wp,-MMD,$(@:.o=.d),-MT,$@ -o $@ $<

$(SPU_CXX_OBJS): $(OBJS_DIR)/%.spu.o: %.cpp
	@mkdir -p $(dir $(@))
	$(SPU_CXX) -c $(SPU_CXXFLAGS) -Wp,-MMD,$(@:.o=.d),-MT,$@ -o $@ $<

$(SPU_AS_OBJS): $(OBJS_DIR)/%.spu.o: %.s
	@mkdir -p $(dir $(@))
	$(SPU_CCAS) -c $(SPU_ASFLAGS) -o $@ $<

$(SPU_BIN_OBJS): $(OBJS_DIR)/%.spu.o: %.bin
	@mkdir -p $(dir $(@))
	$(SPU_OBJCOPY) -I binary -O elf32-spu -B spu \
		--set-section-align .data=7 \
		--set-section-pad .data=128 \
		--rename-section .data=.bin_objs.$< \
		$< $@

$(SPU_ELF_OBJS): $(OBJS_DIR)/%.spu.o: %.elf
	@mkdir -p $(dir $(@))
	$(SPU_OBJCOPY) -I binary -O elf32-spu -B spu \
		--set-section-align .data=7 \
		--set-section-pad .data=128 \
		--rename-section .data=.spu_image.$< \
		$< $@

$(SPU_AR_DEPENDS): $(OBJS_DIR)/%/libtmp.a: %.a
	$(RM) -r $(OBJS_DIR)/$*/
	mkdir -p $(OBJS_DIR)/$*/
	$(CP) $< $(OBJS_DIR)/$*/libtmp.a
	cd $(OBJS_DIR)/$*/ && $(SPU_AR) -x libtmp.a

$(SPU_EMBEDDED_OBJS): $(OBJS_DIR)/%.spu.o: %
	@mkdir -p $(dir $(@))
	$(SPU_OBJCOPY) -I binary -O elf32-spu -B spu \
		--set-section-align .data=7 \
		--set-section-pad .data=128 \
		$< $@

.PHONY: spu-clean

spu-clean:
ifneq ($(SPU_CLEAN_OBJS),)
	$(RM) $(SPU_CLEAN_OBJS)
endif
ifneq ($(strip $(SPU_AR_DEPENDS)),)
	$(RM) -r $(dir $(SPU_AR_DEPENDS))
endif

#----------------------------------------------------------
# include dependency
#----------------------------------------------------------
-include $(SPU_MD_FILES)

